package cn.wolfcode.student.servlet;

import cn.wolfcode.domain.PageResult;
import cn.wolfcode.domain.QueryObject;
import cn.wolfcode.domain.UserQueryObject;
import cn.wolfcode.student.domain.Student;
import cn.wolfcode.student.service.StudentService;
import cn.wolfcode.student.service.impl.StudentServiceImpl;
import cn.wolfcode.student.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setCharacterEncoding("utf-8");
        String userRequest = req.getParameter("userRequest");
        userRequest = userRequest != null ? userRequest : "";
//        Object USER_IN_SESSION = req.getSession().getAttribute("USER_IN_SESSION");
//        if (USER_IN_SESSION == null) {
//            req.setAttribute("logMsg","用户名或密码输入错误");
//            req.getRequestDispatcher("/login.jsp").forward(req, resp);
//            return;
//        }
        switch (userRequest) {
            case "save":
                save(req, resp);
                break;
            case "input":
                input(req, resp);
                break;
            case "delete":
                delete(req, resp);
                break;
            default:
                showAll(req, resp);
                break;
        }
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Student student = new Student();
        String id = req.getParameter("id");
        if (StringUtils.isNotNull(id)) {
            student.setId(Long.valueOf(id));
        }
        String sid = req.getParameter("sid");
        if (StringUtils.isNotNull(sid)) {
            student.setSid(sid);
        }
        String sname = req.getParameter("sname");
        if (StringUtils.isNotNull(sname)) {
            student.setSname(sname);
        }
        String sub_id = req.getParameter("sub_id");
        if (StringUtils.isNotNull(sub_id)) {
            student.setSub_id(Integer.valueOf(sub_id));
        }
        String address = req.getParameter("address");
        if (StringUtils.isNotNull(address)) {
            student.setAddress(address);
        }
        String phone = req.getParameter("phone");
        if (StringUtils.isNotNull(phone)) {
            student.setPhone(phone);
        }
        String email = req.getParameter("email");
        if (StringUtils.isNotNull(email)) {
            student.setEmail(email);
        }
        String age = req.getParameter("age");
        if (StringUtils.isNotNull(age)) {
            student.setAge(Integer.valueOf(age));
        }
        studentService.save(student);
        resp.sendRedirect("/student");
    }

    private void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if (StringUtils.isNotNull(id)) {
            Student student = studentService.selOne(Long.valueOf(id));
            req.setAttribute("student", student);
        }
        req.getRequestDispatcher("/WEB-INF/input.jsp").forward(req, resp);

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        if (StringUtils.isNotNull(id)) {
            studentService.delById(Long.valueOf(id));
        }
        resp.sendRedirect("/student");

    }

    private void showAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        List<Student> students = studentService.selAll();
//        req.setAttribute("students", students);
        UserQueryObject qo = new UserQueryObject();
        String currentPage = req.getParameter("currentPage");
        if (StringUtils.isNotNull(currentPage)) {
            qo.setCurrentPage(Integer.parseInt(currentPage));
        }
        String pageSize = req.getParameter("pageSize");
        if (StringUtils.isNotNull(pageSize)) {
            qo.setPageSize(Integer.parseInt(pageSize));
        }
        String keyword = req.getParameter("keyword");
        if (StringUtils.isNotNull(keyword)) {
            qo.setKeyword(keyword);
        }
        String minAge = req.getParameter("minAge");
        if (StringUtils.isNotNull(minAge)) {
            qo.setMinAge(minAge);
        }
        String maxAge = req.getParameter("maxAge");
        if (StringUtils.isNotNull(maxAge)) {
            qo.setMaxAge(maxAge);
        }

        PageResult<Student> pageResult = studentService.query(qo);
        req.setAttribute("pageResult", pageResult);
        req.setAttribute("qo", qo);
        req.getRequestDispatcher("/WEB-INF/showAll.jsp").forward(req, resp);
    }
}
